# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Setup for build cache

# Avoid duplication if the file happens to be included twice.
if test -z "$bucket" -a -z "$NO_CACHE"; then

# buildbot (or builders that use buildprops.json):
if [ -f $topsrcdir/../buildprops.json ]; then
read branch platform master <<EOF
$(python2.7 -c 'import json; p = json.loads(open("'"$topsrcdir"'/../buildprops.json").read())["properties"]; print p["branch"], p["platform"], p["master"]' 2> /dev/null)
EOF

bucket=
if test -z "$SCCACHE_DISABLE" -a -z "$no_sccache" -a -z "$MOZ_PGO_IS_SET" -a -z "$MOZ_PGO"; then
    case "${branch}" in
    try)
        case "${master}" in
        *scl1.mozilla.com*|*.scl3.mozilla.com*)
            bucket=mozilla-releng-s3-cache-us-west-1-try
            ;;
        *use1.mozilla.com*)
            bucket=mozilla-releng-s3-cache-us-east-1-try
            ;;
        *usw2.mozilla.com*)
            bucket=mozilla-releng-s3-cache-us-west-2-try
            ;;
        esac
        ;;
    autoland|mozilla-inbound)
        case "${master}" in
        *use1.mozilla.com*)
            bucket=mozilla-releng-s3-cache-us-east-1-prod
            ;;
        *usw2.mozilla.com*)
            bucket=mozilla-releng-s3-cache-us-west-2-prod
            ;;
        esac
        ;;
    esac
fi

# builds without buildprops (eg: taskcluster or non-buildbot) and without ccache env config and without sccache disabled:
elif test -z "$CCACHE_DIR" -a -z "$SCCACHE_DISABLE" -a -z "$no_sccache" -a -z "$MOZ_PGO_IS_SET" -a -z "$MOZ_PGO"; then

    # prevent rerun if az is set, or wget is not available
    if test -z "$availability_zone" -a -x "$(command -v wget)"; then
        # timeout after 1 second, and don't retry (failure indicates instance is not in ec2 or network issue)
        # availability_zone is of the form <region><letter> where region is e.g. us-west-2, and az is us-west-2a
        availability_zone=$(wget -T 1 -t 1 -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || true)
        if test -z "$availability_zone" -o "$availability_zone" = "not-ec2"; then
            availability_zone=not-ec2
        else
            # region is az with last letter trimmed
            region=${availability_zone%?}
            # set S3 bucket according to tree (level)
            case "${GECKO_HEAD_REPOSITORY}" in
            *hg.mozilla.org/try*)
                bucket=taskcluster-level-1-sccache-${region}
                ;;
            *hg.mozilla.org/integration/autoland*|*hg.mozilla.org/integration/mozilla-inbound*)
                bucket=taskcluster-level-3-sccache-${region}
                ;;
            esac

            # set a dummy master
            case "${region}" in
            eu-central-1)
                master=dummy.euc1.mozilla.com
                ;;
            us-east-1)
                master=dummy.use1.mozilla.com
                ;;
            us-west-1)
                master=dummy.usw1.mozilla.com
                ;;
            us-west-2)
                master=dummy.usw2.mozilla.com
                ;;
            esac
        fi
    fi
fi

# if platform hasn't been determined from buildprops, and we're on windows,
# it must be set to prevent adding ac_add_options --with-ccache below
if test -z "$platform"; then
    # set platform based on the SYSTEMROOT env var
    case "${SYSTEMROOT}" in
    *Windows)
        platform=windows
        ;;
    esac
fi

if test -z "$bucket"; then
    case "$platform" in
    win*) : ;;
    *)
        ac_add_options --with-ccache
    esac
else
    if ! test -e $topsrcdir/sccache/sccache.py; then
        echo "Sccache missing in the tooltool manifest" >&2
        exit 1
    fi
    mk_add_options "export SCCACHE_BUCKET=$bucket"
    case "$master" in
    *us[ew][12].mozilla.com*|*euc1.mozilla.com*)
        mk_add_options "export SCCACHE_NAMESERVER=169.254.169.253"
        ;;
    esac
    ac_add_options "--with-compiler-wrapper=python2.7 $topsrcdir/sccache/sccache.py"
    mk_add_options MOZ_PREFLIGHT_ALL+=build/sccache.mk
    mk_add_options MOZ_POSTFLIGHT_ALL+=build/sccache.mk
    mk_add_options "UPLOAD_EXTRA_FILES+=sccache.log.gz"
    case "$platform" in
    win*)
        # sccache supports a special flag to create depfiles.
        export _DEPEND_CFLAGS='-deps$(MDDEPDIR)/$(@F).pp'
        # Windows builds have a default wrapper that needs to be overridden
        mk_add_options "export CC_WRAPPER="
        mk_add_options "export CXX_WRAPPER="
        # For now, sccache doesn't support separate PDBs so force debug info to be
        # in object files.
        mk_add_options "export COMPILE_PDB_FLAG="
        mk_add_options "export HOST_PDB_FLAG="
        mk_add_options "export MOZ_DEBUG_FLAGS=-Z7"
        ;;
    esac
fi

fi
